package com.example.demo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.example.demo.context.ContextHolder;
import com.example.demo.context.UserContext;
import com.example.demo.domain.vo.AreaInfo;
import com.example.demo.domain.vo.LoginInfo;
import com.example.demo.domain.vo.LogonUser;
import com.example.demo.domain.vo.SecurityUserService;
import com.example.demo.domain.vo.UserInfoHN;
import com.example.demo.service.LoginService;


/**
 * 登录服务实现
 * @author:feng
 * @version:1.0
 * @date:2018-04-14
 *
 */
@Service
public class LoginServiceHNImpl implements LoginService{
	@Autowired
	private SecurityUserService securityUserService;


   /* @Autowired(required = false)
    public void setSecurityUserService(SecurityUserService securityUserService) {
        this.securityUserService = securityUserService;
    }*/
	@Override
	public LogonUser login(LoginInfo loginInfo) {
		// TODO Auto-generated method stub
		if (StringUtils.isEmpty(loginInfo.getLoginName())) {
            loginInfo.setMessage("用户名不能为空");
            return null;
        }
        if (StringUtils.isEmpty(loginInfo.getPassword())) {
            loginInfo.setMessage("密码不能为空");
            return null;
        }

        //先登录
        LogonUser logonUser = securityUserService.login(loginInfo);
        if (logonUser == null) {
            if(StringUtils.isEmpty(loginInfo.getMessage())){
                loginInfo.setMessage("用户无效或密码错误");
            }
            return null;
        }


        //注入一次用户上下文
        initUserContext(logonUser);

        return logonUser;
		
	}
	/**
     * 初始化上下文对象
     *
     * @param logonUser
     */
    private  UserContext initUserContext(LogonUser logonUser) {
        UserContext userContext = ContextHolder.getUserContext();
        userContext.setUserId(logonUser.getUserId());
        userContext.setUserName(logonUser.getUserName());
        userContext.setOrganId(logonUser.getOrganId());
        userContext.setOrganName(logonUser.getOrganName());
        userContext.setLoginName(logonUser.getLoginName());
        userContext.setDomainId(logonUser.getDomainId());
        userContext.setDomainName(logonUser.getDomainName());
        userContext.setExtContext(logonUser.getExtInfo());
        userContext.put("aaa027", ((UserInfoHN)logonUser.getExtInfo()).getAaa027());
        userContext.put("dstype", "ds_");
        userContext.put("dscode", ((UserInfoHN)logonUser.getExtInfo()).getAaa027());
        userContext.put("code", ((UserInfoHN)logonUser.getExtInfo()).getCode());
        userContext.put("portalUserId", ((UserInfoHN)logonUser.getExtInfo()).getPortalUserId());
        userContext.put("portalUnitId", ((UserInfoHN)logonUser.getExtInfo()).getPortalUnitId());
        if(logonUser.getAreaInfo()!=null){
            AreaInfo areaInfo = logonUser.getAreaInfo();
            userContext.setAreaCode(areaInfo.getAreaCode());
            userContext.setAreaName(areaInfo.getAreaName());

            userContext.setAreaCode1(areaInfo.getAreaCode1());
            userContext.setAreaName1(areaInfo.getAreaName1());

            userContext.setAreaCode2(areaInfo.getAreaCode2());
            userContext.setAreaName2(areaInfo.getAreaName2());

            userContext.setAreaCode3(areaInfo.getAreaCode3());
            userContext.setAreaName3(areaInfo.getAreaName3());

            userContext.setAreaCode4(areaInfo.getAreaCode4());
            userContext.setAreaName4(areaInfo.getAreaName4());

            userContext.setAreaCode5(areaInfo.getAreaCode5());
            userContext.setAreaName5(areaInfo.getAreaName5());

            userContext.setAreaCode6(areaInfo.getAreaCode6());
            userContext.setAreaName6(areaInfo.getAreaName6());
        }
        return userContext;
    }
}
